import { Injectable } from '@angular/core';
import { RestService } from '@abp/ng.core';
import { Observable } from 'rxjs';

export interface ReceiptOrderDto {
  id?: string;
  noticeNo?: string;            // 到货通知单号
  customerNode?: string;        // 客户订单号
  productCode?: string;         // 商品编码
  product?: string;             // 商品名称
  receiver?: string;            // 验收人
  receiptDate?: string;         // 收货日期
  arrivalQuantity?: number;     // 到货数量
  quantity?: number;            // 数量
  boxNote?: string;            // 箱注
  temperature?: string;         // 温度
  productionDate?: string;      // 生产日期
  tray?: string;               // 托盘
  unit?: string;               // 单位
  storageLocation?: string;     // 储位
  volume?: number;             // 体积
  weight?: number;             // 重量
  baseUnit1?: string;          // 基本单位1
  baseUnit2?: string;          // 基本单位2
  quantity1?: number;          // 数量1
  quantity2?: number;          // 数量2
  shipper?: string;            // 货主
  isShelved?: boolean;         // 是否已上架
  selected?: boolean;          // 选中状态
}

export interface PagedResultDto<T> {
  items: T[];
  totalCount: number;
}

@Injectable({
  providedIn: 'root'
})
export class ReceiptOrderService {
  apiName = 'Default';

  constructor(private restService: RestService) {}

  // 获取列表数据
  getList(params: any): Observable<PagedResultDto<ReceiptOrderDto>> {
    return this.restService.request<any, PagedResultDto<ReceiptOrderDto>>({
      method: 'GET',
      url: '/api/app/receipt-order',
      params
    });
  }

  create(input: ReceiptOrderDto): Observable<ReceiptOrderDto> {
    return this.restService.request<any, ReceiptOrderDto>({
      method: 'POST',
      url: '/api/app/receipt-order',
      body: input
    });
  }

  // 删除记录
  delete(id: string): Observable<void> {
    return this.restService.request<any, void>({
      method: 'DELETE',
      url: `/api/app/receipt-order/${id}`
    });
  }

  // 上架操作
  shelfUp(id: string): Observable<void> {
    return this.restService.request<any, void>({
      method: 'POST',
      url: `/api/app/receipt-order/${id}/shelf`
    });
  }

  // 获取商品下拉列表
  getProductList(): Observable<any> {
    return this.restService.request<any, any>({
      method: 'GET',
      url: '/api/app/receipt-order/customer-dropdown-list'
    });
  }

  update(id: string, input: ReceiptOrderDto): Observable<ReceiptOrderDto> {
    return this.restService.request<any, ReceiptOrderDto>({
      method: 'PUT',
      url: `/api/app/receipt-order/${id}`,
      body: input
    });
  }

  getByConditions(params: any) {
    return this.restService.request<any, any>({
      method: 'GET',
      url: '/api/app/receipt-order/by-conditions',
      params
    });
  }

  // 批量上架
  batchShelf(ids: string[]): Observable<void> {
    return this.restService.request<any, void>({
      method: 'POST',
      url: '/api/app/receipt-order/batch-shelf',
      body: ids
    });
  }

  // 导出收货单
  exportReceiptOrders(params?: any): Observable<Blob> {
    return this.restService.request<any, Blob>({
      method: 'POST',
      url: '/api/app/receipt-order/export-receipt-orders',
      body: params,
      responseType: 'blob' as any
    });
  }

  // 导入收货单
  importReceiptOrders(formData: FormData): Observable<void> {
    return this.restService.request<any, void>({
      method: 'POST',
      url: '/api/app/receipt-order/import-receipt-orders',
      body: formData
    });
  }
} 